package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-07-23 10:29:40 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/classesExaminationPaper")
public class ClassesExaminationPaperController extends BaseController {
	@Autowired
	private ClassesExaminationPaperService classesExaminationPaperService;
    @Autowired
    private UserService userService;
    @Autowired
    private TeachingMajorService teachingMajorService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private CoursesService coursesService;
    @Autowired
    private OrganizationNewsService organizationNewsService;
    @Autowired
    private OrganizationRolesService organizationRolesService;
    @Autowired
    private MajorItemBankService majorItemBankService;


    @GetMapping()
	@RequiresPermissions("jzweb:classesExaminationPaper:classesExaminationPaper")
	String ClassesExaminationPaper(){
	    return "jzweb/classesExaminationPaper/classesExaminationPaper";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:classesExaminationPaper:classesExaminationPaper")
	public Result<Page<ClassesExaminationPaperDO>> list(ClassesExaminationPaperDO classesExaminationPaperDTO){
        Wrapper<ClassesExaminationPaperDO> wrapper = new EntityWrapper<ClassesExaminationPaperDO>().orderBy("id", false);
        if(classesExaminationPaperDTO.getStatus()!=null && !classesExaminationPaperDTO.getStatus().equals("")){
            wrapper.eq("status",classesExaminationPaperDTO.getStatus());
        }
        if(classesExaminationPaperDTO.getName()!=null && !classesExaminationPaperDTO.getName().equals("")){
            wrapper.eq("name",classesExaminationPaperDTO.getStatus());
        }

        // 查询列表数据
		Page<ClassesExaminationPaperDO> page = classesExaminationPaperService.selectPage(getPage(ClassesExaminationPaperDO.class),
                wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ClassesExaminationPaperDO obj = (ClassesExaminationPaperDO) page.getRecords().get(i);
            UserDO userDO = userService.selectById(obj.getCreaterId());
            if(userDO!=null && userDO.getUsername()!=null){
                obj.setCreaterName(userDO.getUsername());
            }else {
                obj.setCreaterName("");
            }
            //教学专业名称
            TeachingMajorDO teachingMajorDO = teachingMajorService.selectById(obj.getTeacherMajorId());
            if(teachingMajorDO!=null && teachingMajorDO.getName()!=null){
                obj.setTeacherMajorName(teachingMajorDO.getName());
            }else {
                obj.setTeacherMajorName("");
            }
            //班级名称
            StudentClassesDO studentClassesDO = classesService.selectById(obj.getClassId());
            if(studentClassesDO!=null && studentClassesDO.getName()!=null){
                obj.setStudentclassName(studentClassesDO.getName());
            }else {
                obj.setStudentclassName("");
            }
            //课程名称
            CoursesDO coursesDO = coursesService.selectById(obj.getCoursesId());
            if(coursesDO!=null && coursesDO.getName()!=null){
                obj.setCourseName(coursesDO.getName());
            }else {
                obj.setCourseName("");
            }
            //机构名称
            OrganizationNewsDO org = organizationNewsService.selectById(obj.getOrgId());
            if(org!=null && org.getName()!=null){
                obj.setOrgName(org.getName());
            }else {
                obj.setOrgName("");
            }

            list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:classesExaminationPaper:add")
	String add(Model model){
        Wrapper<TeachingMajorDO> wrapper = new EntityWrapper<TeachingMajorDO>().orderBy("id", false);

        //查询用户角色
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            if(roleName.contains("讲师") && !roleName.contains("培训机构")){
                wrapper.eq("creater_id",this.getUserId());
            }
        }

        if (!roleName.contains("超级用户角色")) {
            List<Map<Object,Object>> list = new ArrayList<>();
            Map<Object,Object> map1 = new HashMap();
            map1.put("id",this.getUser().getOrgId());
            OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(this.getUser().getOrgId());
            if(organizationNewsDO != null){
                map1.put("name",organizationNewsDO.getName());
            }else{
                map1.put("name",this.getUser().getOrgName());
            }
            list.add(map1);
            model.addAttribute("orgNews",list);
        } else {
            model.addAttribute("orgNews",organizationRolesService.selByRoleType(0));
        }

        List<TeachingMajorDO> teachingMajorDOS = teachingMajorService.selectList(wrapper);
        model.addAttribute("teachingMajor",teachingMajorDOS);
	    return "jzweb/classesExaminationPaper/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:classesExaminationPaper:edit")
	String edit(@PathVariable("id") Long id,Model model){
        Wrapper<TeachingMajorDO> wrapper = new EntityWrapper<TeachingMajorDO>().orderBy("id", false);

        //查询用户角色
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            if(roleName.contains("讲师") && !roleName.contains("培训机构")){
                wrapper.eq("creater_id",this.getUserId());
            }
        }
        List<TeachingMajorDO> teachingMajorDOS = teachingMajorService.selectList(wrapper);
        model.addAttribute("teachingMajor",teachingMajorDOS);

        if (!roleName.contains("超级用户角色")) {
            List<Map<Object,Object>> list = new ArrayList<>();
            Map<Object,Object> map1 = new HashMap();
            map1.put("id",this.getUser().getOrgId());
            OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(this.getUser().getOrgId());
            if(organizationNewsDO != null){
                map1.put("name",organizationNewsDO.getName());
            }else{
                map1.put("name",this.getUser().getOrgName());
            }
            list.add(map1);
            model.addAttribute("orgNews",list);
        } else {
            model.addAttribute("orgNews",organizationRolesService.selByRoleType(0));
        }

		ClassesExaminationPaperDO classesExaminationPaper = classesExaminationPaperService.selectById(id);
		model.addAttribute("classesExaminationPaper", classesExaminationPaper);
	    return "jzweb/classesExaminationPaper/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:classesExaminationPaper:add")
	public Result<String> save( ClassesExaminationPaperDO classesExaminationPaper){
        classesExaminationPaper.setCreaterDate(new Date());
        classesExaminationPaper.setCreaterId(this.getUserId());
        classesExaminationPaper.setStatus(0);
		classesExaminationPaperService.insert(classesExaminationPaper);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:classesExaminationPaper:edit")
	public Result<String>  update( ClassesExaminationPaperDO classesExaminationPaper){
        classesExaminationPaper.setUpdaterId(this.getUserId());
		boolean update = classesExaminationPaperService.updateById(classesExaminationPaper);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:classesExaminationPaper:remove")
	public Result<String>  remove( Long id){
        ClassesExaminationPaperDO classesExaminationPaper=this.classesExaminationPaperService.selectById(id);
        if(classesExaminationPaper!=null){
            classesExaminationPaper.setStatus(9);
            classesExaminationPaperService.updateById(classesExaminationPaper);
        }
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:classesExaminationPaper:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
        if (ids.length>0){
            for(int a = 0;a<ids.length;a++){
                ClassesExaminationPaperDO classesExaminationPaper=this.classesExaminationPaperService.selectById(ids[a]);
                if(classesExaminationPaper!=null){
                    classesExaminationPaper.setStatus(9);
                    classesExaminationPaperService.updateById(classesExaminationPaper);
                }
            }
        }
		return Result.ok();
	}

    @Log("修改状态")
    @PostMapping( "/updateStatus")
    @ResponseBody
    @RequiresPermissions("jzweb:classesExaminationPaper:add")
    public Result<String>  updateStatus( Long id, Integer status){
        ClassesExaminationPaperDO classesExaminationPaper=this.classesExaminationPaperService.selectById(id);
        if(classesExaminationPaper!=null){
            classesExaminationPaper.setStatus(status);
            classesExaminationPaperService.updateById(classesExaminationPaper);
        }
        return Result.ok();
    }
	
}
